﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Timers;
using System.Threading;
using System.Net;
using System.Xml;
using Microsoft.Win32;

using LorraineSec.Libraries.Sensors;
using LorraineSec.DatabaseLib.DataObjects;

namespace LorraineSec.ServerApplication
{
    /// <summary>
    /// The GUI that initializes and provides Root access to the ServerApplication.
    /// <remarks>
    /// </remarks>
    /// </summary>
    public partial class AdminGUI : Form
    {

        #region Const/Enum/Instance Variables
        private delegate void AddServerOutputDel(string output);

        private const string REG_FOLDER = "Software\\LorraineSec\\ServerApplication";
        private const string REG_DBFP = "DB_Filepath";

        private ServerApplication servApp;
        private IPAddress ipAddr;
        private IPHostEntry iphost;

        //Temporary Fields
        private string tmpPersonnelPasswordChange;
        private List<Asset> tmpAssetList;

        #endregion

        /// <summary>
        /// Constructs a new GUI and the underlying ServerApplication
        /// </summary>
        public AdminGUI()
        {
            //Load up Sensor Tester
            //LorraineSec.Libraries.Sensors.SensorTest t = new LorraineSec.Libraries.Sensors.SensorTest();
            //t.Show();

            //Setup GUI Components
            InitializeComponent();
            LoadRegistryValues();
            DisableServerAppPanels();           

            //Set boolean so App can peek at key input
            this.KeyPreview = true;

            //Set default IP
            iphost = Dns.GetHostEntry(Dns.GetHostName());
            IPEdit.Text = iphost.AddressList[0].ToString();

            //Attach ListSorters
            ListSelectSorter PersonnelSorter = new ListSelectSorter(UserList, UserSort);
            ListSelectSorter UsergroupSorter = new ListSelectSorter(GroupList, GroupSort);
            ListSelectSorter CompartmentSorter = new ListSelectSorter(CompartmentList, CompartmentSort);
            ListSelectSorter AssetSorter = new ListSelectSorter(AssetList, AssetSort);
            ListSelectSorter GUISorter = new ListSelectSorter(GUI_CompartmentList, GUISort);

            //Finalize Window Display
            this.Focus();
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            //this.WindowState = FormWindowState.Maximized;
        }

        #region Helpers
        private void AddServerOutput(string output)
        {
            outputText.AppendText(output+"\n");
            outputText.SelectionStart = outputText.Text.Length;
            outputText.ScrollToCaret();
        }

        private void LoadRegistryValues()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey(REG_FOLDER);
            DBFileEdit.Text = (string)regKey.GetValue(REG_DBFP, string.Empty);
        }

        private void SaveRegistryValues()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey(REG_FOLDER);
            regKey.SetValue(REG_DBFP, DBFileEdit.Text);
        }

        private void DisableServerAppPanels()
        {
            TabControl.Enabled = false;
        }

        private void EnableServerAppPanels()
        {
            TabControl.Enabled = true;
            PopulateUserList();
            PopulateUsergroupList();
            PopulateCompartmentList();
            PopulateAssetList();
            PopulateDeviceList();
            PopulateLogList();
        }

        private void PopulateUserList()
        {
            User[] userArr = User.GetList(servApp.DataBase).ToArray();
            UserList.Items.Clear();
            UserList.Items.AddRange(userArr);

            AssetUser.Items.Clear();
            AssetUser.Items.AddRange(userArr);
        }

        private void PopulateUsergroupList()
        {
            Usergroup[] groupArr = Usergroup.GetList(servApp.DataBase).ToArray();
            GroupList.Items.Clear();
            GroupList.Items.AddRange(groupArr);

            UserGroup.Items.Clear();
            UserGroup.Items.AddRange(groupArr);
        }

        private void PopulateCompartmentList()
        {
            Compartment[] compArr = Compartment.GetList(servApp.DataBase).ToArray();
            CompartmentList.Items.Clear();
            CompartmentList.Items.AddRange(compArr);

            GroupCompartmentList.Items.Clear();
            GroupCompartmentList.Items.AddRange(compArr);

            AssetSortCompartment.Items.Clear();
            AssetSortCompartment.Items.AddRange(compArr);

            DeviceCompartment.Items.Clear();
            DeviceCompartment.Items.AddRange(compArr);
        }

        private void PopulateAssetList()
        {
            tmpAssetList = Asset.GetList(servApp.DataBase);
            AssetList.Items.Clear();
            AssetList.Items.AddRange(tmpAssetList.ToArray());
        }

        private void PopulateDeviceList()
        {
            Device[] devArr = Device.GetList(servApp.DataBase).ToArray();
            DeviceList.Items.Clear();
            DeviceList.Items.AddRange(devArr.ToArray());
        }

        private void PopulateLogList()
        {
            List<Event> logList = Event.GetList(servApp.DataBase);
        }
        #endregion

        #region Event Handlers   
        #region Misc
        private void ServerOutputCallback(ServerApplication.OutputFlag flag, string output)
        {
            try
            {
                this.Invoke(new AddServerOutputDel(AddServerOutput), new object[] { output });
            }
            catch (Exception)
            {
                //This gets hit on shutdown. Ignore it
            }
        }

        private void TabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TabControl.SelectedTab == GroupTab)
            {
                GroupList.SelectedItems.Clear();
                GroupCompartmentList.SelectedItems.Clear();
            }
        }
        #endregion

        #region Main Controls
        private void loadDBButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select DB File";
            dlg.Multiselect = false;
            dlg.Filter = "Xml File|*.xml";
            dlg.ShowDialog(this);
            DBFileEdit.Text = dlg.FileName;
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            if (servApp == null)
            {
                int port;
                if (ipAddr != null && Int32.TryParse(PortEdit.Text, out port))
                {
                    servApp = new ServerApplication(ipAddr, port, ServerOutputCallback);
                    if (DBFileEdit.Text.Length == 0 || !System.IO.File.Exists(DBFileEdit.Text))
                    {
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Title = "Select Database File";
                        sfd.Filter = "Xml File|*.xml";
                        sfd.ShowDialog();
                        DBFileEdit.Text = sfd.FileName;
                        servApp.init(true, DBFileEdit.Text);
                    }
                    else
                    {
                        servApp.init(false, DBFileEdit.Text);
                    }
                    SaveRegistryValues();
                }
                else
                {
                    MessageBox.Show("Invalid selected IP-Address/Port","ERROR");
                    return;
                }
            }

            if (servApp.serverStatus == Server.Status.IDLE)
            {
                if (servApp.start())
                {
                    EnableServerAppPanels();
                    StartButton.Text = "Stop";
                    //Disable Controls
                    LoadDBButton.Enabled = false;
                    IPEdit.Enabled = false;
                    PortEdit.Enabled = false;
                }
            }
            else if(servApp.serverStatus == Server.Status.RUNNING)
            {
                if (servApp.stop())
                {
                    DisableServerAppPanels();
                    StartButton.Text = "Offline";
                    //Disable Start Button (Require Application Restart)
                    //This is a workaround for socket allocation issues
                    StartButton.Enabled = false;
                }
            }
        }

        private void IPEdit_MouseClick(object sender, MouseEventArgs e)
        {
            object t = ListSelectForm.getSelectionFromList("Select Listen IP", iphost.AddressList, 0);
            if(t != null)
            {
                ipAddr = (IPAddress)t;
                IPEdit.Text = ipAddr.ToString();
            }
        }
        #endregion

        #region Personnel Tab Controls
        private void PersonPasswordButton_Click(object sender, EventArgs e)
        {
            tmpPersonnelPasswordChange = PasswordChanger.PromptChangePassword(null);
        }

        private void RemovePersonButton_Click(object sender, EventArgs e)
        {
            //If any users are selected
            if (UserList.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected personnel?", "Delete Personnel",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    //Loop through each selected user and remove them
                    foreach (User user in UserList.SelectedItems)
                    {
                        servApp.DataBase.removeUser((int)user.ID);
                    }
                    //Refresh Personnel List
                    PopulateUserList();
                }
            }
        }

        private void AddPersonButton_Click(object sender, EventArgs e)
        {      
            //Validate attribute fields
            if (UserUsername.Text.Length > 0 && UserFirst.Text.Length > 0 && UserLast.Text.Length > 0 && UserGroup.SelectedItem != null)
            {
                //Add user to database
                Usergroup grp = (Usergroup)UserGroup.SelectedItem;
                if (servApp.DataBase.addUser(UserUsername.Text, tmpPersonnelPasswordChange, UserRFID.Text, UserFirst.Text, UserLast.Text, (int)grp.ID, 1) != 0)
                {
                    PopulateUserList();
                }
                else
                {
                    MessageBox.Show("Database could not add user.", "ERROR");
                }
            }
            else
            {
                MessageBox.Show("Required Fields Are Unsatisfied", "Add Personnel", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void EditPersonButton_Click(object sender, EventArgs e)
        {
            //If only one user is selected
            if (UserList.SelectedItems.Count == 1 && UserList.SelectedItem != null)
            {
                //Transfer attribute fields
                User user = (User)UserList.SelectedItem;
                user.FirstName = UserFirst.Text;
                user.LastName = UserLast.Text;
                user.UserGroup = ((Usergroup)UserGroup.SelectedItem).ID;
                user.Username = UserUsername.Text;
                if(tmpPersonnelPasswordChange != null)
                    user.Password = tmpPersonnelPasswordChange;
                tmpPersonnelPasswordChange = null;
                user.RFID = UserRFID.Text;
                PopulateUserList();
            }
            else
            {
                MessageBox.Show("Please select a single user", "Edit Personnel");
            }
        }

        private void PersonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //If only one user is selected
            if (UserList.SelectedItems.Count == 1 && UserList.SelectedItem != null)
            {
                //Update attribute fields
                User user = (User)UserList.SelectedItem;
                //Populate temporary fields
                UserID.Text = user.ID.ToString();
                UserFirst.Text = user.FirstName;
                UserLast.Text = user.LastName;
                UserRFID.Text = user.RFID;
                UserUsername.Text = user.Username;
                Usergroup sel = servApp.DataBase.getUsergroupById((int)user.UserGroup);
                foreach (Usergroup grp in UserGroup.Items)
                {
                    if (grp.ID == sel.ID)
                        UserGroup.SelectedItem = grp;
                }
            }
            else
            {
                UserID.Text = string.Empty;
                UserFirst.Text = string.Empty;
                UserLast.Text = string.Empty;
                UserRFID.Text = string.Empty;
                UserUsername.Text = string.Empty;
                UserGroup.SelectedItem = null;
            }
        }
        #endregion  

        #region Usergroup Tab Controls
        private void EditGroupButton_Click(object sender, EventArgs e)
        {
            //If only one group is selected
            if (GroupList.SelectedItems.Count == 1 && GroupList.SelectedItem != null)
            {
                //Transfer attribute fields
                Usergroup grp = (Usergroup)GroupList.SelectedItem;
                grp.Name = GroupName.Text;
                List<Compartment> compList = new List<Compartment>(GroupCompartmentList.SelectedItems.Count);
                foreach (Compartment cmp in GroupCompartmentList.SelectedItems)
                {
                    compList.Add(cmp);
                }
                grp.SetCompartments(servApp.DataBase, compList);
                PopulateUsergroupList();
            }
            else
            {
                MessageBox.Show("Please select a single usergroup", "Edit Usergroup");
            }
        }

        private void AddGroupButton_Click(object sender, EventArgs e)
        {
            //Validate attribute fields
            if (GroupName.Text.Length > 0 && GroupCompartmentList.SelectedIndices.Count > 0)
            {
                //Add group to database
                int id = servApp.DataBase.addUsergroup(GroupName.Text);
                if (id != 0)
                {
                    Usergroup grp = servApp.DataBase.getUsergroupById(id);
                    List<Compartment> compList = new List<Compartment>(GroupCompartmentList.SelectedItems.Count);
                    foreach(Compartment cmp in GroupCompartmentList.SelectedItems)
                    {
                        compList.Add(cmp);
                    }
                    grp.SetCompartments(servApp.DataBase, compList);
                    PopulateUsergroupList();
                }
                else
                {
                    MessageBox.Show("Database could not add user.", "ERROR");
                }
            }
            else
            {
                MessageBox.Show("Required Fields Are Unsatisfied", "Add Usergroup", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void RemoveGroupButton_Click(object sender, EventArgs e)
        {
            //If any groups are selected
            if (GroupList.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected usergroup?", "Usergroup",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    //Loop through and remove each selected group
                    foreach (Usergroup group in GroupList.SelectedItems)
                    {
                        if (servApp.DataBase.getUsersByUsergroup((int)group.ID) != null)
                        {
                            MessageBox.Show("Unable to remove Usergroup: " + group.Name + ". Users still assigned to it");
                        }
                        else
                        {
                            servApp.DataBase.removeUsergroup((int)group.ID);
                        }
                    }
                    //Refresh Personnel List
                    PopulateUsergroupList();
                }
            }
        }

        private void GroupList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //If only one group is selected
            if (GroupList.SelectedItems.Count == 1 && GroupList.SelectedItem != null)
            {
                Usergroup grp = (Usergroup)GroupList.SelectedItem;
                //Populate temporary fields
                GroupName.Text = grp.Name;

                //Loop through referenes compartments for usergroup
                GroupCompartmentList.SelectedItems.Clear();
                foreach (Compartment comp in grp.GetCompartments(servApp.DataBase))
                {
                    //Select nodes in list
                    foreach (Compartment compare in GroupCompartmentList.Items)
                    {
                        if (comp.ID == compare.ID)
                        {
                            GroupCompartmentList.SelectedItems.Add(compare);
                            break;
                        }
                    }
                }
            }
            else
            {
                GroupName.Text = string.Empty;
                GroupCompartmentList.SelectedItems.Clear();
            }
        }
        #endregion

        #region Compartment Tab Controls
        private void CompartmentEditButton_Click(object sender, EventArgs e)
        {
            if (CompartmentList.SelectedItems.Count == 1 && CompartmentList.SelectedItem != null)
            {
                Compartment cmp = (Compartment)CompartmentList.SelectedItem;
                cmp.Name = CompartmentName.Text;
                cmp.Passcode = CompartmentPasscode.Text;
                PopulateCompartmentList();
            }
            else
            {
                MessageBox.Show("Please select a single compartment", "Edit Compartment");
            }
        }

        private void CompartmentAddButton_Click(object sender, EventArgs e)
        {
            if (CompartmentName.Text.Length > 0)
            {
                if (servApp.DataBase.addCompartment(CompartmentName.Text,CompartmentPasscode.Text) != 0)
                {
                    PopulateCompartmentList();
                }
                else
                {
                    MessageBox.Show("Database could not add compartment.", "ERROR");
                }
            }
            else
            {
                MessageBox.Show("Required Fields Are Unsatisfied", "Add Compartment", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void CompartmentRemoveButton_Click(object sender, EventArgs e)
        {
            if (CompartmentList.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected compartment?", "Compartment",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    foreach (Compartment group in CompartmentList.SelectedItems)
                    {
                        if (false)//This can be ignored but could lead to ghost references in database
                        {
                            MessageBox.Show("Unable to remove Usergroup: " + group.Name + ". Usergroup still referencing it");
                        }
                        else
                        {
                            servApp.DataBase.removeCompartment((int)group.ID);
                        }
                    }
                    //Refresh Personnel List
                    PopulateCompartmentList();
                }
            }
        }

        private void CompartmentList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CompartmentList.SelectedItems.Count == 1)
            {
                Compartment cmp = (Compartment)CompartmentList.SelectedItem;
                //Populate temporary fields
                CompartmentName.Text = cmp.Name;
                CompartmentPasscode.Text = cmp.Passcode;
            }
            else
            {
                CompartmentName.Text = string.Empty;
                CompartmentPasscode.Text = string.Empty;
            }
        }
        #endregion

        #region Asset Tab Controls

        private void AssetSortCompartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssetList.Items.Clear();
            Compartment cmp = (Compartment)AssetSortCompartment.SelectedItem;
            //Select items from root temp list
            foreach (Asset ast in tmpAssetList)
            {
                if(ast.CompartmentID == cmp.ID)
                    AssetList.Items.Add(ast);
            }
        }

        private void EditAssetButton_Click(object sender, EventArgs e)
        {
            if(AssetList.SelectedItems.Count == 1 && AssetList.SelectedItem != null)
            {
                Asset ast = (Asset)AssetList.SelectedItem;
                ast.Name = AssetName.Text;
                if (AssetUser.SelectedItem == null)
                    ast.UserID = 0;
                else
                    ast.UserID = ((User)AssetUser.SelectedItem).ID;
                ast.CompartmentID = ((Compartment)AssetSortCompartment.SelectedItem).ID;
                ast.RFID = AssetRFID.Text;
                PopulateAssetList();
            }
            else
            {
                MessageBox.Show("Please select a single asset", "Edit Asset");
            }
        }

        private void AddAssetButton_Click(object sender, EventArgs e)
        {
            if (AssetName.Text.Length > 0 && AssetSortCompartment.SelectedItem != null)
            {
                Compartment cmp = (Compartment)AssetSortCompartment.SelectedItem;
                User usr = (User)AssetUser.SelectedItem;  
                int userID = 0;
                if (usr != null)
                    userID = (int)usr.ID;
                if (servApp.DataBase.addAsset(AssetRFID.Text, AssetName.Text, (int)cmp.ID, userID) != 0)
                {
                    PopulateAssetList();
                }
                else
                {
                    MessageBox.Show("Database could not add asset.", "ERROR");
                }
            }
            else
            {
                MessageBox.Show("Required Fields Are Unsatisfied", "Add Asset", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void RemoveAssetButton_Click(object sender, EventArgs e)
        {
            if (AssetList.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected assets?", "Remove Asset",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    foreach (Asset ast in AssetList.SelectedItems)
                    {
                        servApp.DataBase.removeAssetById((int)ast.ID);
                    }
                }
            }
        }

        private void AssetList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AssetList.SelectedItems.Count == 1)
            {
                Asset sel = (Asset)AssetList.SelectedItem;
                //Populate temporary fields
                AssetName.Text = sel.Name;
                AssetRFID.Text = sel.RFID;
                if (sel.UserID == 0)
                    AssetUser.SelectedItem = null;
                else
                {
                    foreach (User usr in AssetUser.Items)
                    {
                        if (usr.ID == sel.UserID)
                            AssetUser.SelectedItem = usr;
                    }
                }
            }
            else
            {
                AssetName.Text = string.Empty;
                AssetUser.SelectedItem = null;
            }
        }

        private void AssetCheckInButton_Click(object sender, EventArgs e)
        {
            AssetUser.SelectedItem = null;
        }

        private void AssetChangeCompButton_Click(object sender, EventArgs e)
        {
            if (AssetList.SelectedItems.Count == 1)
            {
                List<Compartment> compList = Compartment.GetList(servApp.DataBase);
                Compartment comp = (Compartment)ListSelectForm.getSelectionFromList("Select Compartment", compList.ToArray(), 0);
                if (comp != null)
                {
                    Asset ast = (Asset)AssetList.SelectedItem;
                    ast.CompartmentID = comp.ID;
                    foreach (Compartment compSort in AssetSortCompartment.Items)
                    {
                        if(compSort.ID == comp.ID)
                            AssetSortCompartment.SelectedItem = compSort;
                    }
                }
            }
            else
            {
                MessageBox.Show("Please select a single asset", "Edit Asset");
            }
        }
        #endregion

        #region Device Tab Controls
        private void EditDeviceButton_Click(object sender, EventArgs e)
        {
            if (AssetList.SelectedItems.Count == 1 && AssetList.SelectedItem != null)
            {
                Device dev = (Device)DeviceList.SelectedItem;
                dev.Name = DeviceName.Text;
                dev.CompartmentID = ((Compartment)DeviceCompartment.SelectedItem).ID;
                PopulateDeviceList();
            }
            else
            {
                MessageBox.Show("Please select a single device", "Edit Device");
            }
        }

        private void AddDeviceButton_Click(object sender, EventArgs e)
        {
            if (DeviceName.Text.Length > 0 && DeviceCompartment.SelectedItem != null)
            {
                //Show Add Device GUI
                Compartment cmp = (Compartment)DeviceCompartment.SelectedItem;
                AddDeviceForm.ShowAddDevicePrompt(DeviceName.Text, cmp.ID, AddDeviceCallback);
                AddDeviceButton.Enabled = false;
                AddDeviceButton.Text = "Adding...";
            }
            else
            {
                MessageBox.Show("Required Fields Are Unsatisfied", "Add Device", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void AddDeviceCallback(AddDeviceForm.AddDeviceInfo info)
        {
            //Add device to database
            int id = servApp.DataBase.addDevice(info.Name, info.Type, (int)info.CompartmentID, info.Serial, (int)info.LibID);
            if (id != 0)
            {
                //Add device to lists
                Device dev = servApp.DataBase.getDeviceById(id);
                servApp.SensorDeviceMap.Add(info.SensorLibrary, dev);
                servApp.Sensors.Add(dev.Serial, info.SensorLibrary);//is this being used?

                //Subscribe to library
                info.SensorLibrary.subscribe(servApp.SensorRecvCallback);
                AddDeviceButton.Enabled = true;
                AddDeviceButton.Text = "Add Device";
            }
        }

        private void RemoveDeviceButton_Click(object sender, EventArgs e)
        {
            if (DeviceList.SelectedItems.Count > 0)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected devices?", "Remove Device",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    foreach (Device dev in DeviceList.SelectedItems)
                    {
                        servApp.DataBase.removeDevice((int)dev.ID);
                    }
                }
            }
        }

        private void DeviceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DeviceList.SelectedItems.Count == 1)
            {
                Device dev = (Device)DeviceList.SelectedItem;
                DeviceName.Text = dev.Name;
                DeviceType.Text = dev.Type;
                DeviceSerial.Text = dev.Serial;
                DeviceLibID.Text = dev.LibID.ToString();
                DeviceID.Text = dev.ID.ToString();
                DeviceValue.Text = "UNKNOWN";
                foreach(Compartment cmp in DeviceCompartment.Items)
                {
                    if (cmp.ID == dev.CompartmentID)
                        DeviceCompartment.SelectedItem = cmp;
                }
            }
            else
            {
                DeviceName.Text = string.Empty;
                DeviceType.Text = string.Empty;
                DeviceSerial.Text = string.Empty;
                DeviceCompartment.SelectedItem = null;  
                DeviceValue.Text = string.Empty;
                DeviceLibID.Text = string.Empty;
                DeviceID.Text = string.Empty;
            }
        }
        #endregion

        #region Log List Controls
        private void Log_List_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Log_ID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Log_UserID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Log_CompartmentID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Log_AssetID_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        #endregion        

        #region GUI Tab Controls

        private void GUI_CompartmentList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void GUI_DeviceList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void GUISort_TextChanged(object sender, EventArgs e)
        {

        }

        private void GUI_IP_TextChanged(object sender, EventArgs e)
        {

        }

        #endregion
        #endregion
    }
}
